﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using HCMServices.DTO;
using System.Collections.Generic;

namespace HCMServices.DAO
{
    public class DetailDataDAO
    {
        private MyDataContext db;

        public DetailDataDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        public List<DetailDataDTO> Seach(int dataId)
        {
            List<DetailDataDTO> detail = new List<DetailDataDTO>();

            var query = from CHITIET_DULIEU dtl in db.DetailData
                        where dtl.MaDuLieu == dataId
                        select dtl;

            ConvertToDTO(detail, query);
            
            return detail;
        }

        private static void ConvertToDTO(List<DetailDataDTO> detail, IQueryable<CHITIET_DULIEU> query)
        {
            foreach (CHITIET_DULIEU d in query)
            {
                DetailDataDTO temp = new DetailDataDTO();
                temp.DataID = d.MaDuLieu;
                temp.ServiceID = d.MaChiTiet;

                detail.Add(temp);
            }
        }

        public List<DetailDataDTO> GetAllDetail()
        {
            List<DetailDataDTO> allDetail = new List<DetailDataDTO>();

            var query = from CHITIET_DULIEU dtl in db.DetailData
                        select dtl;

            ConvertToDTO(allDetail, query);

            return allDetail;
        }

        public int GetIdByService(int serviceId)
        {
            int dataId = -1;

            var result = from CHITIET_DULIEU dtl in db.DetailData
                         where dtl.MaChiTiet == serviceId
                         select dtl.MaDuLieu;
            if (result != null)
            {
                dataId = result.First();
            }

            return dataId;
        }
    }
}
